package com.example.demo.biz;

import com.example.demo.dao.IEhistoryDao;
import com.example.demo.dao.IEinfoDao;
import com.example.demo.dao.IExpressDao;
import com.example.demo.pojo.Einfo;
import com.example.demo.pojo.Express;
import com.example.demo.util.Dateutil;
import com.example.demo.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

import static java.lang.System.out;

/**
 * Created by Administrator on 2017/6/3.
 */
@Service
public class ExpressBiz {
    @Autowired
    private IEhistoryDao ehistoryDao;
    @Autowired
    private IEinfoDao einfoDao;
    @Autowired
    private IExpressDao expressDao;
    @Transactional
    public Express addExpress(Express express){
        Express save = expressDao.save(express);
        List<Einfo> einfos=new ArrayList<>();
        //获取详情条数
        int getno=0;
        switch (save.getType()) {
            case 4:
                getno = RandomUtil.getno(12, 1);
                break;
            case 3:
                getno = RandomUtil.getno(9, 1);
                break;
            case 2:
                getno = RandomUtil.getno(6, 1);
                break;
            case 1:
                getno = RandomUtil.getno(3, 1);
                break;
        }
        //获取最后更新日期
        Date date1 = Dateutil.TimeToDate(save.getTime());
        //获取快递随机发送天数
        int getno2 = RandomUtil.getno(7, 1);
        //获取随机天数前的日期
        String afterDayDate = Dateutil.getAfterDayDate(-getno2 + "", date1);
        //给据开始结束时间生成随机时间

        int getno1 = RandomUtil.getno(38, 1);
        //确保时间唯一性
        HashSet<Date> dates = new HashSet<>();
        ArrayList<Date> dates1 = new ArrayList<>();
        while (dates.size()!=getno){
            //获取随机时间
            Date date = RandomUtil.randomDate(afterDayDate, Dateutil.DatetoString(date1));
            dates.add(date);
        }
        for (Date date : dates) {
            dates1.add(date);
        }
        dates1.sort(Date::compareTo);
        out.println("+++++++++++"+dates1.size());
        for (int i = 0; i < getno; i++) {
            out.println("第"+i+"个时间："+dates1.get(i));
            Einfo einfo = new Einfo();
            einfo.setEid(save.getEid());
            einfo.setContent(getno1+i+1+"");
            einfo.setTime(Dateutil.DateToTime(dates1.get(i)));
            einfos.add(einfo);
        }
        List<Einfo> save1 = einfoDao.save(einfos);
        out.println("成功增加了"+save1.size()+"条详情");
        return save;
    }
}
